package com.foreveross.project.usercenterserver.oauth2.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.foreveross.project.usercenterserver.common.constant.ResultCode;
import com.foreveross.project.usercenterserver.common.utils.R;
import com.foreveross.project.usercenterserver.exception.CustomException;
import com.foreveross.project.usercenterserver.oauth2.domain.UcApp;
import com.foreveross.project.usercenterserver.oauth2.dto.AppDto;
import com.foreveross.project.usercenterserver.oauth2.service.IUcAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yz
 * @since 2019-07-19
 */
@Api(description = "应用管理")
@RestController
@RequestMapping("/oauth2/app")

public class UcAppController {


    @Autowired
    private IUcAppService ucAppService;

    /**
     * 添加应用
     */
    @PostMapping(value = "/createApp")
    @ApiOperation(value = "创建应用", notes = "成功返回 UcApp 实体")
    public R createApp(
            @Valid @RequestBody AppDto appDto) {
        UcApp app = null;
        try {
            app = ucAppService.createApp(appDto);
            return R.ok(app);
        } catch (CustomException e) {
            return R.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改应用
     */
    @PostMapping(value = "/updateApp")
    @ApiOperation(value = "修改应用", notes = "成功返回 UcApp 实体")
    public R updateApp(
            @Valid @RequestBody AppDto appDto) {
        UcApp app = null;
        try {
            app = ucAppService.updateApp(appDto);
            return R.ok(app);
        } catch (Exception e) {
            //e.printStackTrace();
            return R.error(ResultCode.BAD_REQUEST.getCode(), e.getMessage());
        }
    }

    /**
     * 删除应用
     */
    @DeleteMapping(value = "/deleteApp/{appFkId}")
    @ApiOperation(value = "删除应用", notes = "成功返回code=200")
    public R deleteApp(
            @PathVariable String appFkId
    ) {
        try {
            ucAppService.deleteApp(appFkId);
            return R.ok();
        } catch (Exception e) {
            return R.error(ResultCode.BAD_REQUEST.getCode(), e.getMessage());
        }
    }

    /**
     * 查询单个应用
     *
     * @param appFkId
     * @return
     */
    @GetMapping(value = "/detail/{appFkId}")
    @ApiOperation(value = "查询单个应用", notes = "成功返回 UcApp 实体")
    public R appDetail(
            @PathVariable String appFkId
    ) {
        try {
            return R.ok(ucAppService.findApp(appFkId));
        } catch (Exception e) {
            return R.error(ResultCode.BAD_REQUEST.getCode(), e.getMessage());
        }
    }


    /**
     * 应用列表
     */
    @GetMapping(value = "/appList")
    @ApiOperation(value = "应用列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", dataType = "Integer", defaultValue = "1", required = false, value = "当前页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", dataType = "Integer", defaultValue = "10", required = false, value = "每页记录数", paramType = "query"),
            @ApiImplicitParam(name = "tenantFkId", dataType = "String", required = true, value = "租户FkId", paramType = "query"),
            @ApiImplicitParam(name = "appName", dataType = "String", required = false, value = "应用名称", paramType = "query"),
            @ApiImplicitParam(name = "delFlag", dataType = "Integer", defaultValue = "0", required = false, value = "是否删除  -1：已删除  0：正常", paramType = "query"),
    })
    public R appList(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = true) String tenantFkId,
            @RequestParam(required = false) String appName,
            @RequestParam(required = false) Integer delFlag
    ) {
        //查询条件
        Map<String, Object> params = new HashMap<>();
        params.put("appName", appName);
        params.put("tenantFkId", tenantFkId);
        params.put("delFlag", delFlag);
        IPage<UcApp> dtoIPage = null;
        try {
            dtoIPage = ucAppService.queryForPage(new Page<>(pageNum, pageSize), params);
            return R.ok(dtoIPage);
        } catch (Exception e) {
            return R.error(ResultCode.BAD_REQUEST.getCode(), e.getMessage());
        }
    }


}
